package com.springboot.system.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.springboot.system.dao.PermissionDao;
import com.springboot.system.dao.RoleDao;
import com.springboot.system.domain.auth.Permission;
import com.springboot.system.domain.auth.RolePermission;
import com.springboot.common.exception.SystemException;
import com.springboot.common.utils.BeanUtils;
import com.springboot.common.utils.StringUtils;

/**
 * 权限业务层实现类
 * 
 * @ClassName: PermissionService
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 252956
 * @date 2021年1月7日 上午9:51:49
 *
 */
@Service
public class PermissionService {

	@Autowired
	private PermissionDao permissionDao;
	@Autowired
	private RoleDao roleDao;

	public List<Permission> queryAll() {
		return permissionDao.selectAll();
	}

	public List<Permission> selectByRole(Integer[] roleIds) {
		if (roleIds == null) {
			throw new SystemException("角色roleIds为空");
		}
		List<Permission> owendRightList = permissionDao.selectByRole(roleIds);
		if (owendRightList == null || owendRightList.isEmpty()) {
			return null;
		}
		Set<Permission> ownedSet = new HashSet<Permission>();
		for (Permission right : owendRightList) {
			ownedSet.add(right);
		}
		owendRightList = new ArrayList<Permission>(ownedSet);
		return owendRightList;
	}

	public boolean add(Permission permission) {
		if (permission.getTypes() == null) {
			throw new SystemException("权限类型不能为空");
		}
		if (StringUtils.isBlank(permission.getName())) {
			throw new SystemException("菜单标题/按钮名称不能为空");
		}
		if (permission.getTypes() != 2) {
			if (StringUtils.isBlank(permission.getUrl())) {
				throw new SystemException("路由地址不能为空");
			}
		}
		if (permission.getTypes() == 1 && permission.getI_frame() == 0) {
			if (StringUtils.isBlank(permission.getComponent_name())) {
				throw new SystemException("组件名不能为空");
			}
			if (StringUtils.isBlank(permission.getComponent_path())) {
				throw new SystemException("组件路径不能为空");
			}
		}

		if (permission.getParentId() == null) {
			permission.setParentId(0);
		}
		int result = permissionDao.insert(permission);
		if (result < 0) {
			throw new SystemException("操作失败");
		}
		return true;
	}

	public boolean update(Permission permission) {
		if (permission.getTypes() == null) {
			throw new SystemException("权限类型不能为空");
		}
		if (StringUtils.isBlank(permission.getName())) {
			throw new SystemException("菜单标题/按钮名称不能为空");
		}
		if (permission.getTypes() != 2) {
			if (StringUtils.isBlank(permission.getUrl())) {
				throw new SystemException("路由地址不能为空");
			}
		}
		if (permission.getTypes() == 1 && permission.getI_frame() == 0) {
			if (StringUtils.isBlank(permission.getComponent_name())) {
				throw new SystemException("组件名不能为空");
			}
			if (StringUtils.isBlank(permission.getComponent_path())) {
				throw new SystemException("组件路径不能为空");
			}
		}
		if (permission.getParentId() == null) {
			permission.setParentId(0);
		}
		Permission permissionInfo = permissionDao.selectById(permission.getId(), Permission.class);
		if (permissionInfo == null) {
			throw new SystemException("没有对应的权限");
		}
		BeanUtils.copyObject(permissionInfo, permission);
		int result = permissionDao.update(permissionInfo);
		if (result < 0) {
			throw new SystemException("执行失败");
		}
		return true;
	}

	@Transactional(value = "baseTransactionManager")
	public boolean delete(Integer id) {
		if (id == null) {
			throw new SystemException("删除条件不能为空");
		}
		// 删除角色权限
		roleDao.delete(new RolePermission().setPermId(id));

		// 删除权限本身
		permissionDao.delete(new Permission().setId(id));

		return true;
	}

}
